//
//  CronRadiusViewController.m
//  性能优化
//
//  Created by apple on 2022/1/15.
//

#import "CronRadiusViewController.h"
#import "SDWebImage.h"

@interface CronRadiusCell : UITableViewCell

@property(nonatomic,strong)UIImageView * iconView;
@property(nonatomic,strong)UILabel * t_label;

@property(nonatomic,strong)UIImageView * iconRightView;
@property(nonatomic,strong)UIImageView * iconRightView2;
@end

@implementation CronRadiusCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.iconView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 40, 40)];
        [self.contentView addSubview:self.iconView];
        self.iconView.layer.cornerRadius =20;
        self.iconView.layer.masksToBounds =YES;
        self.iconView.layer.shouldRasterize =YES;
        
        CGFloat width = [UIScreen mainScreen].bounds.size.width;
        
        self.iconRightView = [[UIImageView alloc]initWithFrame:CGRectMake(width-120, 10, 40, 40)];
        [self.contentView addSubview:self.iconRightView];
        self.iconRightView.layer.cornerRadius =20;
        self.iconRightView.layer.masksToBounds =YES;
        self.iconRightView.layer.shouldRasterize =YES;
        
        self.iconRightView2 = [[UIImageView alloc]initWithFrame:CGRectMake(width-40, 10, 40, 40)];
        [self.contentView addSubview:self.iconRightView2];
        self.iconRightView2.layer.cornerRadius =20;
        self.iconRightView2.layer.masksToBounds =YES;
        self.iconRightView2.layer.shouldRasterize =YES;
        
        
        self.t_label =[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_iconView.frame)+10, 0, 200, 60)];
        [self.contentView addSubview:self.t_label];
    }
    return self;
}


@end

static NSString * const CornCellID = @"CornCellID";

@interface CronRadiusViewController ()<UITableViewDelegate,UITableViewDataSource>

//tableView
@property(nonatomic,strong)UITableView * tableView;
//数据源
@property(nonatomic,strong)NSMutableArray * dataSource;

@property(nonatomic,strong)CADisplayLink * displayLink;

@end

@implementation CronRadiusViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    self.tableView.rowHeight =60;
    [self.tableView registerClass:[CronRadiusCell class] forCellReuseIdentifier:CornCellID];
    
    [self loadData];
    
    NSLog(@"RetainCount:%ld",(long)CFGetRetainCount((__bridge CFTypeRef)self));
    _displayLink =[CADisplayLink displayLinkWithTarget:self selector:@selector(displayLinkMethod:)];
    NSLog(@"RetainCount:%ld",(long)CFGetRetainCount((__bridge CFTypeRef)self));
    _displayLink.preferredFramesPerSecond =60;
    [_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
}

-(void)dealloc{
    NSLog(@"%s",__func__);
    
}

-(void)displayLinkMethod:(CADisplayLink*)displayLink{
    static float lastTime =0;//上次时间
    static int count =0;
   
    if (lastTime==0) {
        lastTime =displayLink.timestamp;
        return;
    }
    count++;
    
   CGFloat duration = displayLink.timestamp-lastTime;
    
    if (duration>1) {
        NSLog(@"进来了");
       
        NSLog(@"count：%d",count);
        NSLog(@"duration：%f",duration);
        NSInteger fps = count/duration;
        NSLog(@"fps:%ld",(long)fps);
        
        lastTime = displayLink.timestamp;
        count = 0;

        
        return;
    }
    
    
    
    
    
    
}

-(void)loadData{
    
    NSArray * temp = [self.class readLocalFileWithName:@"totalList"];
     [self.dataSource addObjectsFromArray:temp];
    [self.tableView reloadData];
}

// 读取本地JSON文件
+ (NSArray *)readLocalFileWithName:(NSString *)name {
    // 获取文件路径
    NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"json"];
    // 将文件数据化
    NSData *data = [[NSData alloc] initWithContentsOfFile:path];
    // 对数据进行JSON格式化并返回字典形式
    return [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
}

-(NSMutableArray *)dataSource{
    if (!_dataSource ) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CronRadiusCell * cell = [tableView dequeueReusableCellWithIdentifier:CornCellID];
    if (!cell) {
        cell = [[CronRadiusCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CornCellID];
    }
    NSDictionary * dict = [self.dataSource objectAtIndex:indexPath.row];
    NSArray * fodderPicList = [dict objectForKey:@"fodderPicList"];
    if (fodderPicList&&fodderPicList.count) {
        if (cell.iconView) {
            [cell.iconView removeFromSuperview];
            [cell.contentView addSubview:cell.iconView];
        }
        sleep(0.5);
        [cell.iconView sd_setImageWithURL:[NSURL URLWithString:[@"https:" stringByAppendingString:[fodderPicList firstObject]]]];
        [cell.iconRightView sd_setImageWithURL:[NSURL URLWithString:[@"https:" stringByAppendingString:[fodderPicList firstObject]]]];
        [cell.iconRightView2 sd_setImageWithURL:[NSURL URLWithString:[@"https:" stringByAppendingString:[fodderPicList firstObject]]]];
    }
    cell.t_label.text =[dict objectForKey:@"fodderContent"];
    
    return cell;
}

//-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    [_displayLink removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
//    self.displayLink = nil;
////    [_displayLink invalidate];
//
//}








@end
